package com.irdstudio.bhcredit.core.assembly.plugin.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据表操作类-[表名: 03.平台子系统数据源信息表(s_subs_datasource)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-09-16
 */
public class SSubsDatasourceDao {

	private static final Logger logger = LoggerFactory.getLogger(SSubsDatasourceDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public SSubsDatasourceDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public SSubsDatasource queryWithKeys(String subsCode,String subsDsCode)
			throws SQLException {
		SSubsDatasource dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM s_subs_datasource WHERE subs_code=? and subs_ds_code=?");
			ps.setString(1,subsCode);
			ps.setString(2,subsDsCode);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new SSubsDatasource();
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setSubsDsCode(rs.getString("subs_ds_code"));
				dc.setSubsDsName(rs.getString("subs_ds_name"));
				dc.setSubsDsType(rs.getString("subs_ds_type"));
				dc.setDsDbName(rs.getString("ds_db_name"));
				dc.setDsConnStr(rs.getString("ds_conn_str"));
				dc.setDsUserId(rs.getString("ds_user_id"));
				dc.setDsUserPwd(rs.getString("ds_user_pwd"));
				dc.setDsSchemaName(rs.getString("ds_schema_name"));
			}
		} catch (SQLException e) {
			throw new SQLException("querySSubsDatasourceWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<SSubsDatasource> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<SSubsDatasource> dcList = new ArrayList<SSubsDatasource>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM s_subs_datasource "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				SSubsDatasource dc = new SSubsDatasource();
//				dc.setSubsCode(rs.getString("subs_code"));
//				dc.setSubsDsCode(rs.getString("subs_ds_code"));
//				dc.setSubsDsName(rs.getString("subs_ds_name"));
//				dc.setSubsDsType(rs.getString("subs_ds_type"));
//				dc.setDsDbName(rs.getString("ds_db_name"));
//				dc.setDsConnStr(rs.getString("ds_conn_str"));
//				dc.setDsUserId(rs.getString("ds_user_id"));
//				dc.setDsUserPwd(rs.getString("ds_user_pwd"));
//				dc.setDsSchemaName(rs.getString("ds_schema_name"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("querySSubsDatasourceWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}
	
	/**
	 * @description:查询所有
	 * @return
	 * @throws SQLException
	 * @date:2018年2月19日 上午11:42:35
	 * @author:hengyh
	 */
	public List<SSubsDatasource> queryAll()
			throws SQLException {
		List<SSubsDatasource> dcList = new ArrayList<SSubsDatasource>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM s_subs_datasource ");
			rs = ps.executeQuery();
			while (rs.next()) {
				SSubsDatasource dc = new SSubsDatasource();
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setSubsDsCode(rs.getString("subs_ds_code"));
				dc.setSubsDsName(rs.getString("subs_ds_name"));
				dc.setSubsDsType(rs.getString("subs_ds_type"));
				dc.setDsDbName(rs.getString("ds_db_name"));
				dc.setDsConnStr(rs.getString("ds_conn_str"));
				dc.setDsUserId(rs.getString("ds_user_id"));
				dc.setDsUserPwd(rs.getString("ds_user_pwd"));
				dc.setDsSchemaName(rs.getString("ds_schema_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("querySSubsDatasourceWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
